package com.quick.develop.business.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.quick.develop.framework.common.annotation.CreateForm;
import com.quick.develop.framework.common.annotation.DetailForm;
import com.quick.develop.framework.common.annotation.QueryTable;
import com.quick.develop.framework.common.annotation.UpdateForm;
import lombok.AllArgsConstructor;
import com.quick.develop.framework.common.utils.PageResult;
import com.quick.develop.framework.mybatis.service.impl.BaseServiceImpl;
import com.quick.develop.business.convert.EnvironmentalMonitoringConfigurationConvert;
import com.quick.develop.business.entity.EnvironmentalMonitoringConfigurationEntity;
import com.quick.develop.business.query.EnvironmentalMonitoringConfigurationQuery;
import com.quick.develop.business.vo.EnvironmentalMonitoringConfigurationVO;
import com.quick.develop.business.dao.EnvironmentalMonitoringConfigurationDao;
import com.quick.develop.business.service.EnvironmentalMonitoringConfigurationService;
import com.quick.develop.form.utils.ExcelUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 环境监测阈值配置
 *
 * @author GJH 1906559468@qq.com
 * @since 1.0.0 2024-01-27
 */
@Service
@AllArgsConstructor
public class EnvironmentalMonitoringConfigurationServiceImpl extends BaseServiceImpl<EnvironmentalMonitoringConfigurationDao, EnvironmentalMonitoringConfigurationEntity> implements EnvironmentalMonitoringConfigurationService {

    @QueryTable
    @Override
    public PageResult<EnvironmentalMonitoringConfigurationVO> page(EnvironmentalMonitoringConfigurationQuery query) {
        IPage<EnvironmentalMonitoringConfigurationEntity> page = baseMapper.selectPage(getPage(query), getWrapperOptional(query));

        return new PageResult<>(EnvironmentalMonitoringConfigurationConvert.INSTANCE.convertList(page.getRecords()), page.getTotal());
    }

    private LambdaQueryWrapper<EnvironmentalMonitoringConfigurationEntity> getWrapper(EnvironmentalMonitoringConfigurationQuery query) {
        LambdaQueryWrapper<EnvironmentalMonitoringConfigurationEntity> wrapper = Wrappers.lambdaQuery();
        return wrapper;
    }

    private LambdaQueryWrapper<EnvironmentalMonitoringConfigurationEntity> getWrapperOptional(EnvironmentalMonitoringConfigurationQuery query) {
        LambdaQueryWrapper<EnvironmentalMonitoringConfigurationEntity> wrapper = Wrappers.lambdaQuery();
            wrapper.eq(!StrUtil.isBlankIfStr(query.getId()), EnvironmentalMonitoringConfigurationEntity::getId, query.getId());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getBusinessKey()), EnvironmentalMonitoringConfigurationEntity::getBusinessKey, query.getBusinessKey());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getAttachId()), EnvironmentalMonitoringConfigurationEntity::getAttachId, query.getAttachId());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getDataSource()), EnvironmentalMonitoringConfigurationEntity::getDataSource, query.getDataSource());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getDeviceLocation()), EnvironmentalMonitoringConfigurationEntity::getDeviceLocation, query.getDeviceLocation());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getRedLightCondition()), EnvironmentalMonitoringConfigurationEntity::getRedLightCondition, query.getRedLightCondition());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getRedLightLimit()), EnvironmentalMonitoringConfigurationEntity::getRedLightLimit, query.getRedLightLimit());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getYellowLightCondition()), EnvironmentalMonitoringConfigurationEntity::getYellowLightCondition, query.getYellowLightCondition());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getYellowLightLimit()), EnvironmentalMonitoringConfigurationEntity::getYellowLightLimit, query.getYellowLightLimit());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getGreenLightCondition()), EnvironmentalMonitoringConfigurationEntity::getGreenLightCondition, query.getGreenLightCondition());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getGreenLightLimit()), EnvironmentalMonitoringConfigurationEntity::getGreenLightLimit, query.getGreenLightLimit());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getAlarmStatus()), EnvironmentalMonitoringConfigurationEntity::getAlarmStatus, query.getAlarmStatus());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getEarlyWarnStatus()), EnvironmentalMonitoringConfigurationEntity::getEarlyWarnStatus, query.getEarlyWarnStatus());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getRiseLimit()), EnvironmentalMonitoringConfigurationEntity::getRiseLimit, query.getRiseLimit());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getUnit()), EnvironmentalMonitoringConfigurationEntity::getUnit, query.getUnit());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getRemark()), EnvironmentalMonitoringConfigurationEntity::getRemark, query.getRemark());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getCreatedUserCode()), EnvironmentalMonitoringConfigurationEntity::getCreatedUserCode, query.getCreatedUserCode());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getCreatedUserName()), EnvironmentalMonitoringConfigurationEntity::getCreatedUserName, query.getCreatedUserName());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getCreatedTime()), EnvironmentalMonitoringConfigurationEntity::getCreatedTime, query.getCreatedTime());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getUpdatedUserCode()), EnvironmentalMonitoringConfigurationEntity::getUpdatedUserCode, query.getUpdatedUserCode());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getUpdatedUserName()), EnvironmentalMonitoringConfigurationEntity::getUpdatedUserName, query.getUpdatedUserName());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getUpdatedTime()), EnvironmentalMonitoringConfigurationEntity::getUpdatedTime, query.getUpdatedTime());
            wrapper.eq(!StrUtil.isBlankIfStr(query.getDelFlag()), EnvironmentalMonitoringConfigurationEntity::getDelFlag, query.getDelFlag());
            wrapper.like(!StrUtil.isBlankIfStr(query.getIdLike()), EnvironmentalMonitoringConfigurationEntity::getId, query.getIdLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getBusinessKeyLike()), EnvironmentalMonitoringConfigurationEntity::getBusinessKey, query.getBusinessKeyLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getAttachIdLike()), EnvironmentalMonitoringConfigurationEntity::getAttachId, query.getAttachIdLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getDataSourceLike()), EnvironmentalMonitoringConfigurationEntity::getDataSource, query.getDataSourceLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getDeviceLocationLike()), EnvironmentalMonitoringConfigurationEntity::getDeviceLocation, query.getDeviceLocationLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getRedLightConditionLike()), EnvironmentalMonitoringConfigurationEntity::getRedLightCondition, query.getRedLightConditionLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getRedLightLimitLike()), EnvironmentalMonitoringConfigurationEntity::getRedLightLimit, query.getRedLightLimitLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getYellowLightConditionLike()), EnvironmentalMonitoringConfigurationEntity::getYellowLightCondition, query.getYellowLightConditionLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getYellowLightLimitLike()), EnvironmentalMonitoringConfigurationEntity::getYellowLightLimit, query.getYellowLightLimitLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getGreenLightConditionLike()), EnvironmentalMonitoringConfigurationEntity::getGreenLightCondition, query.getGreenLightConditionLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getGreenLightLimitLike()), EnvironmentalMonitoringConfigurationEntity::getGreenLightLimit, query.getGreenLightLimitLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getAlarmStatusLike()), EnvironmentalMonitoringConfigurationEntity::getAlarmStatus, query.getAlarmStatusLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getEarlyWarnStatusLike()), EnvironmentalMonitoringConfigurationEntity::getEarlyWarnStatus, query.getEarlyWarnStatusLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getRiseLimitLike()), EnvironmentalMonitoringConfigurationEntity::getRiseLimit, query.getRiseLimitLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getUnitLike()), EnvironmentalMonitoringConfigurationEntity::getUnit, query.getUnitLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getRemarkLike()), EnvironmentalMonitoringConfigurationEntity::getRemark, query.getRemarkLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getCreatedUserCodeLike()), EnvironmentalMonitoringConfigurationEntity::getCreatedUserCode, query.getCreatedUserCodeLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getCreatedUserNameLike()), EnvironmentalMonitoringConfigurationEntity::getCreatedUserName, query.getCreatedUserNameLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getCreatedTimeLike()), EnvironmentalMonitoringConfigurationEntity::getCreatedTime, query.getCreatedTimeLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getUpdatedUserCodeLike()), EnvironmentalMonitoringConfigurationEntity::getUpdatedUserCode, query.getUpdatedUserCodeLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getUpdatedUserNameLike()), EnvironmentalMonitoringConfigurationEntity::getUpdatedUserName, query.getUpdatedUserNameLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getUpdatedTimeLike()), EnvironmentalMonitoringConfigurationEntity::getUpdatedTime, query.getUpdatedTimeLike()).or();
            wrapper.like(!StrUtil.isBlankIfStr(query.getDelFlagLike()), EnvironmentalMonitoringConfigurationEntity::getDelFlag, query.getDelFlagLike()).or();
        return wrapper;
    }

    @CreateForm
    @Override
    public void save(EnvironmentalMonitoringConfigurationVO vo) {
        EnvironmentalMonitoringConfigurationEntity entity = EnvironmentalMonitoringConfigurationConvert.INSTANCE.convert(vo);

        baseMapper.insert(entity);
    }

    @UpdateForm
    @Override
    public void update(EnvironmentalMonitoringConfigurationVO vo) {
        EnvironmentalMonitoringConfigurationEntity entity = EnvironmentalMonitoringConfigurationConvert.INSTANCE.convert(vo);

        updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<String> idList) {
        removeByIds(idList);
    }

    @Override
    public EnvironmentalMonitoringConfigurationVO getEnvironmentalMonitoringConfigurationVO(String id) {
    EnvironmentalMonitoringConfigurationEntity entity = getById(id);
        return EnvironmentalMonitoringConfigurationConvert.INSTANCE.convert(entity);
    }

    @DetailForm
    @Override
    public EnvironmentalMonitoringConfigurationVO getEnvironmentalMonitoringConfigurationByBusinessKey(String businessKey) {
        EnvironmentalMonitoringConfigurationEntity entity = baseMapper.selectOne(new LambdaQueryWrapper<EnvironmentalMonitoringConfigurationEntity>()
                .eq(EnvironmentalMonitoringConfigurationEntity::getBusinessKey, businessKey));
        return EnvironmentalMonitoringConfigurationConvert.INSTANCE.convert(entity);
    }

    @Override
    public List<EnvironmentalMonitoringConfigurationVO> getListByKeys(EnvironmentalMonitoringConfigurationQuery query) {
        return this.getListByKeys(query.getFieldName(), query.getCodes());
    }

    @Override
    public void dynamicExportExcel(EnvironmentalMonitoringConfigurationQuery query) {
        List<EnvironmentalMonitoringConfigurationEntity> list = this.list(getWrapperOptional(query));
        ExcelUtils.downloadExcel(EnvironmentalMonitoringConfigurationEntity.class, list, "环境监测阈值配置导出");
    }

    @Override
    public void dynamicImportExcel(MultipartFile file) {
        ExcelUtils.execWriteDatabase(
                file,
                EnvironmentalMonitoringConfigurationEntity.class,
                this::filterDynamicImport,
                this::overrideDynamicImport,
                this::updateBatchById,
                this::saveBatch
        );
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void batchSave(List<EnvironmentalMonitoringConfigurationVO> list) {
        if (CollUtil.isNotEmpty(list)) {
            List<EnvironmentalMonitoringConfigurationEntity> entities = list.stream().map(EnvironmentalMonitoringConfigurationConvert.INSTANCE::convert).collect(Collectors.toList());
            saveBatch(entities);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void batchUpdateById(List<EnvironmentalMonitoringConfigurationVO> list) {
        if (CollUtil.isNotEmpty(list)) {
            List<EnvironmentalMonitoringConfigurationEntity> entities = list.stream().map(EnvironmentalMonitoringConfigurationConvert.INSTANCE::convert).collect(Collectors.toList());
            updateBatchById(entities);
        }
    }

    public List<EnvironmentalMonitoringConfigurationVO> getListByKeys(String key, Collection<Serializable> keys) {
        if (CollUtil.isNotEmpty(keys) && StrUtil.isNotEmpty(key)) {
            return Collections.emptyList();
        }
        return EnvironmentalMonitoringConfigurationConvert.INSTANCE.convertList(list(Wrappers.query(EnvironmentalMonitoringConfigurationEntity.class).in(key, keys)));
    }

    /**
    *
    * @param sFunction 函数式获取Key
    * @param keys 需要查询的列表
    * @return 返回数据列表
    */
    public List<EnvironmentalMonitoringConfigurationEntity> getListByKeys(SFunction<EnvironmentalMonitoringConfigurationEntity, ?> sFunction, Collection<Serializable> keys) {
        if (CollUtil.isNotEmpty(keys)) {
            return Collections.emptyList();
        }
        return list(Wrappers.lambdaQuery(EnvironmentalMonitoringConfigurationEntity.class).in(sFunction, keys));
    }

    /**
     * @param list 解析的数据
     * @return 过滤以后的数据
     */
    public List<EnvironmentalMonitoringConfigurationEntity> filterDynamicImport(List<EnvironmentalMonitoringConfigurationEntity> list) {
        return list;
    }

    /**
     * @param list 解析的数据
     * @return 返回过滤的数据
     * @description 覆盖导入的数据
     */
    public List<EnvironmentalMonitoringConfigurationEntity> overrideDynamicImport(List<EnvironmentalMonitoringConfigurationEntity> list) {
        return Collections.emptyList();
    }
}